import { Injectable } from '@nestjs/common'
import { CreatePostDto } from './dto/create-post.dto'
import { UpdatePostDto } from './dto/update-post.dto'
import { UpdatePostItemDto } from './dto/update-postItem.dto'
import { Post } from 'src/post/entities/post.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Like } from 'typeorm'
import { User } from 'src/user/entities/user.entity'
import { CreateCommitDto } from './dto/create-commit.dto'
import { Commit } from 'src/commit/entities/commit.entity'
@Injectable()
export class PostService {
  constructor(
    @InjectRepository(Post) private readonly post: Repository<Post>,
    @InjectRepository(User) private readonly user: Repository<User>,
    @InjectRepository(Commit) private readonly commit: Repository<Commit>
  ) {}
  //发表文章
  create(createPostDto: CreatePostDto) {
    console.log(createPostDto)
    const data = new Post()
    data.title = createPostDto.title
    data.content = createPostDto.content
    return this.post.save(data)
  }
  //发表评论
  async createCommit(createCommitDto: CreateCommitDto) {
    const postInfo = await this.post.findOne({
      relations: ['commits'],
      where: { id: createCommitDto.postId },
    })
    console.log(postInfo)
    if (postInfo && createCommitDto.userId) {
      const commitList: Commit[] = postInfo.commits
      let C = new Commit()
      C.content = createCommitDto.content
      C.userId = createCommitDto.userId
      commitList.push(await this.commit.save(C))
      console.log(commitList)
      postInfo.commits = commitList
      this.post.save(postInfo)
    } else {
      console.log('帖子或用户不存在')
      return
    }
  }
  async findByName(query) {
    const titleRes = await this.post.find({
      relations: ['commits', 'user'],
      where: { title: Like(`%${query.name}%`) },
    })
    const contentRes = await this.post.find({
      relations: ['commits', 'user'],
      where: { content: Like(`%${query.name}%`) },
    })
    return { ...titleRes, ...contentRes }
  }
  async find(query) {
    const data = await this.post.find({
      // where: {
      //   title: Like(`%${query.keyword}%`),
      // },
      relations: ['user', 'commits'],
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
      skip: (query.page - 1) * query.pageSize, //偏移量   相当于数据库里的limit
      take: query.pageSize,
    })
    const total = await this.post.count({
      // where: {
      //   title: Like(`%${query.keyword}%`),
      // },
    })
    return { data, total }
  }
  async findAll() {
    const data = await this.post.find({
      relations: ['user', 'commits'],
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
    })

    return data
  }

  //根据id查询
  findOne(id: string) {
    return this.post.findOne({
      relations: ['user', 'commits'],
      where: {
        id: id,
      },
    })
  }
  //根据用户id查询
  async findByUser(query) {
    let data = []
    let total = 0
    const dataList = await this.post.find({
      relations: ['user', 'commits'],
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
      skip: (query.page - 1) * query.pageSize, //偏移量   相当于数据库里的limit
      take: query.pageSize,
    })
    for (let i = 0; i < dataList.length; i++) {
      const element = dataList[i]
      if (element.user.id === query.userId) {
        data.push(element)
      }
    }

    const totalList = await this.post.find({
      relations: ['user', 'commits'],
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
    })
    for (let i = 0; i < totalList.length; i++) {
      const element = totalList[i]
      if (element.user.id === query.userId) {
        total++
      }
    }

    return { data, total }
  }

  //根据idList查询
  async findByUserList(query) {
    let data = []
    let total = 0
    const dataList = await this.post.find({
      relations: ['user', 'commits'],
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
      skip: (query.page - 1) * query.pageSize, //偏移量   相当于数据库里的limit
      take: query.pageSize,
    })
    for (let i = 0; i < dataList.length; i++) {
      const element = dataList[i]
      if (query.userIdList.includes(element.user.id)) {
        data.push(element)
      }
    }

    const totalList = await this.post.find({
      relations: ['user', 'commits'],
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
    })
    for (let i = 0; i < totalList.length; i++) {
      const element = totalList[i]
      if (query.userIdList.includes(element.user.id)) {
        total++
      }
    }

    return { data, total }
  }
  //根据话题圈子查询
  async findByTopicName(query) {
    console.log(query)

    const data = await this.post.find({
      relations: ['user', 'commits'],
      where: {
        topic: Like(`${query.topic}`),
      },
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
      skip: (query.page - 1) * query.pageSize, //偏移量   相当于数据库里的limit
      take: query.pageSize,
    })

    const total = await this.post.count({
      relations: ['user', 'commits'],
      where: {
        topic: Like(`${query.topic}`),
      },
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
    })

    return { data, total }
  }

  async updateEye(id: string, updatePostDto: UpdatePostDto) {
    console.log(id)
    let data = await this.post.findOne({ where: { id: id } })
    if (data && updatePostDto.eye) {
      data.eye++
      return this.post.save(data)
    } else if (data && updatePostDto.thumb) {
      data.thumb++
      return this.post.save(data)
    } else {
      return
    }
  }
  async updatePostItem(id: string, updatePostItemDto: UpdatePostItemDto) {
    let data = await this.post.findOne({ where: { id: id } })
    if (data && updatePostItemDto.title) {
      data.title = updatePostItemDto.title
    }
    if (data && updatePostItemDto.content) {
      data.content = updatePostItemDto.content
    }
    if (data && updatePostItemDto.topic) {
      data.topic = updatePostItemDto.topic
    }
    if (data && updatePostItemDto.imgArray) {
      data.imgArray = updatePostItemDto.imgArray
    }

    console.log(data)

    return this.post.save(data)
  }

  remove(id: string) {
    return this.post.delete(id)
  }
  removeCommit(id: string) {
    return this.commit.delete(id)
  }
}
